package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.JournalLogin;
import com.example.service.JournalLoginService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;


@RestController
@RequestMapping("journalLogins")
public class JournalLoginController {

    @Autowired
    private JournalLoginService journalLoginService;

    /**
     * 分页查询所有登录操作日志信息
     * @return
     */
    @GetMapping("/getPageJournalLogin/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,JournalLogin journalLogin){

        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<JournalLogin> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(journalLogin.getJobNumber()),JournalLogin::getJobNumber,journalLogin.getJobNumber());

        IPage page1 = journalLoginService.page(page,lqw);

        return R.success("分页查询所有journalLogins成功！",page1);
    }


    /**
     * 添加
     * @param journalLogin
     * @return
     */
    @PostMapping("/addJournalLogin")
    public R add(@RequestBody JournalLogin journalLogin){
        System.out.println("==========");
        System.out.println(journalLogin);
        System.out.println("==========");

        Date date = new Date();   //获取当前的日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String dateNow = simpleDateFormat.format(date);//获取String类型的时间
        journalLogin.setTime(dateNow);

        Boolean update = journalLoginService.save(journalLogin);
        return R.success("添加journalLogin成功!", null);
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteJournalLogin/{id}")
    public R deleteById(@PathVariable int id){

        Boolean byId = journalLoginService.removeById(id);
        if (byId){
            return R.success("删除journalLogins成功!",null);
        }
        return R.error("删除journalLogins失败!");
    }


}

